package x.ovo.wechat.bot.plugin;

import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.convert.ConvertUtil;
import org.dromara.hutool.core.text.StrUtil;
import picocli.CommandLine;
import x.ovo.wechat.bot.core.Constant;
import x.ovo.wechat.bot.core.command.CommandExcutor;
import x.ovo.wechat.bot.core.plugin.Plugin;

import java.util.List;
import java.util.Map;

@Slf4j(topic = "Scheduler")
@CommandLine.Command(name = "scheduler", description = "定时任务管理")
public class SchedulerExecutor extends CommandExcutor {
    public SchedulerExecutor(Plugin plugin) {
        super(plugin);
    }

    @CommandLine.Command(name = "add", description = "添加定时任务")
    public void add(
            @CommandLine.Option(names = {"-n", "--name"}, description = "任务名称", required = true) String name,
            @CommandLine.Option(names = {"-c", "--cron"}, description = "cron表达式，空格需要使用[-+|.]代替", required = true) String cron,
            @CommandLine.Option(names = {"-k", "--keyword"}, description = "关键词", required = true) String keyword,
            @CommandLine.Option(names = {"-t", "--targets"}, description = "目标", required = true) List<String> targets
    ) {
        SchedulerConfig config = SchedulerConfig.builder().name(name).cron(cron.replaceAll("[-+|.]", " ")).keyword(keyword).targets(targets).build();
        this.plugin.getConfig().put(name, BeanUtil.beanToMap(config));
        this.plugin.saveConfig();
        log.info("添加定时任务 [{}] 成功", name);
    }

    @CommandLine.Command(name = "remove", description = "删除定时任务")
    public void delete(@CommandLine.Parameters(paramLabel = "name", description = "任务名称", arity = "1..*") List<String> names) {
        names.forEach(this.plugin.getConfig().keySet()::remove);
        this.plugin.saveConfig();
        log.info("删除定时任务 [{}] 成功", names);
    }

    @CommandLine.Command(name = "list", description = "列出定时任务")
    public String list() {
        StringBuilder sb = new StringBuilder();
        this.plugin.getConfig().forEach((name, config) -> {
            SchedulerConfig convert = ConvertUtil.convert(SchedulerConfig.class, config);
            String s = StrUtil.format("任务名称: {}\ncron: {}\n关键词: {}\n目标: {}", name, convert.getCron(), convert.getKeyword(), convert.getTargets());
            sb.append(s).append(Constant.delimiter);
        });
        return sb.toString();
    }

    @CommandLine.Command(name = "edit", description = "编辑定时任务", sortOptions = false)
    public void edit(
            @CommandLine.Option(names = {"-n", "--name"}, description = "任务名称", required = true) String name,
            @CommandLine.Option(names = {"-c", "--cron"}, description = "cron表达式，空格需要使用[-+|.]代替") String cron,
            @CommandLine.Option(names = {"-k", "--keyword"}, description = "关键词") String keyword,
            @CommandLine.Option(names = {"-t", "--targets"}, description = "目标", split = ",") List<String> targets
    ) {
        this.plugin.<Map<String, Object>>getConfig().computeIfPresent(name, (k, v) -> {
            if (cron != null) v.put("cron", cron.replaceAll("[-+|.]", " "));
            if (keyword != null) v.put("keyword", keyword);
            if (targets != null) v.put("targets", targets);
            return v;
        });
        this.plugin.saveConfig();
    }
}
